package cn.xinfei.xdecision.data.core.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;


@Slf4j
@Service
public class ExpressionUtil {

    private static List<String> BASIC_DATA_TYPES = Arrays.asList("integer", "double", "boolean", "string");
    /**
     * 数据类型校验
     *
     * @param desiredType 支持："integer","double","boolean","string"
     * @param obj         需要校验的数据
     * @return 校验结果
     */
    public static boolean resultDataTypeCheck(String desiredType, Object obj) {
        if (BASIC_DATA_TYPES.contains(desiredType)) {
            switch (desiredType) {
                case "integer":
                    if (obj instanceof BigDecimal) {
                        return true;
                    }
                    return obj instanceof Integer;
                case "double":
                    if (obj instanceof BigDecimal) {
                        return true;
                    }
                    return obj instanceof Double;
                case "string":
                    return obj instanceof String;
                case "boolean":
                    return obj instanceof Boolean;
            }
        }
        return false;
    }

    /**
     * 数据类型校验
     *
     * @param desiredType   支持："integer","double","boolean","string"
     * @param desiredResult 期望的结果
     * @param obj           需要校验的数据
     * @return 校验结果
     */
    public static Boolean resultDataCheck(String desiredType, String desiredResult, Object obj) {
        if (obj == null) {
            return false;
        }
        //默认按照字符串进行校验
        if (StringUtils.isBlank(desiredType)) {
            desiredType = "string";
        }
        if (BASIC_DATA_TYPES.contains(desiredType)) {
            switch (desiredType) {
                case "integer":
                    if (obj instanceof BigDecimal) {
                        BigDecimal result = (BigDecimal) obj;
                        if (result.intValue() == Integer.parseInt(desiredResult)) {
                            return true;
                        }
                    } else if (obj instanceof Integer) {
                        Integer result = (Integer) obj;
                        if (result.equals(Integer.valueOf(desiredResult))) {
                            return true;
                        }
                    }

                case "double":
                    if (obj instanceof BigDecimal) {
                        BigDecimal result = (BigDecimal) obj;
                        if (result.doubleValue() == Double.parseDouble(desiredResult)) {
                            return true;
                        }
                    } else if (obj instanceof Double) {
                        Double result = (Double) obj;
                        if (result.equals(Double.valueOf(desiredResult))) {
                            return true;
                        }
                    }
                case "string":
                    if (obj instanceof String) {
                        String result = (String) obj;
                        if (result.equals(desiredResult)) {
                            return true;
                        }
                    } else {
                        //兼容默认值
                        String result = obj.toString();
                        if (result.equals(desiredResult)) {
                            return true;
                        }
                    }
                case "boolean":
                    if (obj instanceof Boolean) {
                        Boolean result = (Boolean) obj;
                        if (result.equals(Boolean.valueOf(desiredResult))) {
                            return true;
                        }
                    }
            }
        }
        return false;
    }
}
